﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using WebApplication1.Classes;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Collections.Generic;

namespace WebApplication1
{
    public partial class Default : System.Web.UI.Page
    {
        private User u1;
        private string Сon = @"Data Source=LOCALHOST\SQLEXPRESS;Initial Catalog=Test;
Integrated Security=True;Pooling=False";
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void Connection(string c)
        {
            try
            {
                SqlConnection connection = new SqlConnection(c);
                connection.Open();
            }
            catch (SqlException ex)
            {
                Console.WriteLine("Ошибка при установлении соединения: " + ex.Message);
            }
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            Panel1.Visible = true;
        }
        protected void Button1_Click(object sender, EventArgs e)
        {

            SqlConnection connection = new SqlConnection(Сon);
            connection.Open();
            try
            {
                List<User> userList = new List<User>();
                SqlCommand сommand2 = new SqlCommand("select * from User1 where Password='" + TextBox2.Text + "' and Login='" + TextBox1.Text + "'", connection);
                SqlDataReader reader = сommand2.ExecuteReader();

                while (reader.Read())
                {
                    userList.Add(new User(Convert.ToInt32(reader["Id"]), Convert.ToString(reader["Password"]), Convert.ToInt32(reader["Role"]), Convert.ToString(reader["Name"]), Convert.ToString(reader["Surname"]), Convert.ToInt32(reader["Phone"]), Convert.ToString(reader["Email"]), Convert.ToString(reader["Login"])));
                }

                reader.Close();

                if (userList.Count() == 1)
                {
                    u1 = userList.First();
                    Session["id"] = u1.Id;
                    Session["name"] = u1.Name;
                    Session["surname"] = u1.Surname;
                    Session["role"] = u1.Role;
                    //GridView1.AutoGenerateEditButton = true;
                    //GridView1.AutoGenerateDeleteButton = true;
                    Button3.Visible = true;
                    Button4.Visible = false;
                    Label2.Visible = false;
                    TextBox1.Visible = false;
                    TextBox2.Visible = false;
                    Label5.Visible = false;
                    Label6.Visible = false;
                    Button1.Visible = false;
                    
                    
                    Button2.Visible = true;
                    Label4.Visible = false;
                    if (u1.Role != 1)
                    {
                        Button4.Visible = true;
                    }
                }
                else
                {
                    Button3.Visible = false;
                    Button4.Visible = false;
                    Label4.Visible = true;
                }
            }
            catch (SqlException ex)
            {
                Console.WriteLine("Ошибка при выборке данных: " + ex.Message);


            }
            connection.Close();
            Server.Transfer("Main.aspx");
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            Label5.Visible = true;
            Label6.Visible = true;
            Label2.Visible = true;
            TextBox1.Visible = true;
            TextBox1.Text = "";
            TextBox2.Visible = true;
            TextBox2.Text = "";
            Button1.Visible = true;
            Button3.Visible = false;
            Button4.Visible = false;
            Button2.Visible = false;
            //GridView1.AutoGenerateEditButton = false;
            //GridView1.AutoGenerateDeleteButton = false;

        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            Session["id"] = 1111;
            Session["name"] = "";
            Session["surname"] = "Anonimous";
            Session["role"] = 0;
            Server.Transfer("Main.aspx");
        }
    }
}
